//index.js
//获取应用实例
const app = getApp()
var help = require('../../utils/help.js');
var login = require('../../utils/login.js');

Page({
  data: {
    imageUrl: app.data.imageUrl,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),

    //轮播图
    imgarr: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,

    //tab切换
    tabActive1:"tabactive",
    tabActive3:"",

    data: [],
    page: 1, // 当前页
    is_load: true, // 是否继续加载
    load_lock: false, // 加载锁
    load_ts: '',
    keyword: '',
    cate_id: 3,

    config:{}
  },
  
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (option) {
    console.log('option:', option);
    if (!app.data.userInfo) {
      login.getUserInfo(app);
    }
    if (option.uid) {
      console.log('option.uid:', option);
      help.setCache('scene', option.uid);
    }
    if (option.scene) {
      var q_scene = help.decodeScene(option.scene)
      var uid = q_scene.uid ? q_scene.uid : 0;
      console.log('scene.uid:', uid);
      help.setCache('scene', uid);
    }
  },
  onShow: function () {
    app.getConfig(this);
    var show_page_cate_id = help.getCache('show_page_cate_id');
    if (show_page_cate_id) {
      this.setData({
        cate_id: show_page_cate_id
      })
      help.delCache('show_page_cate_id');
    }
    this.loadData(true);
    this.loadBaseData();
  },
  //tab切换函数
  onclicktab: function (e) {

    this.setData({
      cate_id: e.currentTarget.dataset.id
    })
    this.loadData(true);
  },

  //tab切换函数
  dianzan: function (e) {
    var that = this;
    help.get(app.data.apiUrl + 'Index/addzan', {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      a_id: e.currentTarget.dataset.gid
    }, that, function (res, that) {
      var result = res.data;
      // console.log(result);
      if (result.code == 1) {
        that.setData({
        
        })
      } else {
        help.msg(result.msg)
      }

    });
    console.log(e.currentTarget.dataset.gid)
  },
  /**
   * 幻灯片跳转
   */
  showImgUrl: function (e) {
    wx.navigateTo({
      url: e.target.dataset.url
    })
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    var that = this;
    // 加载数据
    app.getConfig(this);
    that.loadData(true);
    that.loadBaseData();
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1000)
  },
  set_keyword: function (e) {
    this.setData({
      keyword: e.detail.value
    })
    this.loadData(true);
  },
  loadBaseData: function () {
    var that = this;
    //幻灯片
    help.get(app.data.apiUrl + 'Index/getBanner', {}, that, function (res, that) {
      var result = res.data;
      // console.log(result);
      if (result.code == 1) {
        that.setData({
          imgarr: result.data,
        })
      }

    });
  },
  /**
   * 加载数据
   */
  loadData: function (is_one) {
    var that = this;

    if (is_one === true) {
      that.setData({
        is_load: true
      })
    }
    if (this.data.load_lock || !this.data.is_load) {
      return false;
    }
    this.setData({
      load_lock: true,
      load_ts: '正在加载'
    })
    if (is_one === true) {
      that.setData({
        page: 1,
        data: []
      })
    }

    var url = app.data.apiUrl + 'Index/doclist';
    var data = {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      keyword: that.data.keyword,
      page: that.data.page,
      cate_id: that.data.cate_id
    };
    help.get(url, data, that, function (res, that) {
      that.setData({
        load_lock: false
      })
      var result = res.data;
      console.log(result);
      if (result.code == 1) {
        if (result.data.length > 0) {
          if (is_one === true) {
            var new_data = result.data;
          } else {
            var new_data = that.data.data.concat(result.data)
          }
          console.log(new_data)
          that.setData({
            data: new_data,
            load_ts: ''
          })
        } else {
          if (is_one === true) {
            that.setData({
              data: [],
            })
          }
          that.setData({
            is_load: false,
            load_ts: '没有更多数据了',

          })
        }
        that.data.page++;

      } else if (result.code == 401) {
        help.msg('登录已失效，正在重新登录...')
        // 重新检测登录状态
        login.getUserInfo(app);
        return false;
      } else {
        help.msg(result.msg)
        return false;
      }

    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadData();
  },
  goCouponType: function(){
    help.goUrl('/pages/coupon_type/coupon_type')
  }
})
